import MonoSingle from "../Utility/MonoSingle";
import BaseInputEvent from "./BaseInputEvent";
import TvBoxButtonListEvent from "./TvBoxButtonListEvent";
import GetScale from "../Utility/GetScale";
const { ccclass, property } = cc._decorator;

export enum inputKeyCode {
	left = 1000,
	right = 1001,
	up = 1003,
	down = 1004,
	enter = 13,
	enter2 = 1005,
	quit = 6,
	//数字键
	num0 = 48,
	num1 = 49,
	num2 = 50,
	num3 = 51,
	num4 = 52,
	num5 = 53,
	num6 = 54,
	num7 = 55,
	num8 = 56,
	num9 = 57,
}

export enum inputDirectionKey {
	left = 1001,
	right = 1002,
	up = 1003,
	down = 1004,
	enter = 1005,
	none = 1000
}

@ccclass
export default class TvBoxInput extends cc.Component {
	@property
	curBtn: cc.Button = null;
	@property
	curPanel: cc.Node = null;
	@property
	btn_guide: cc.Button = null;
	@property
	btn_select: cc.Button = null;
	isCanMoveForword: boolean = true;
	curInputKey: number = null;
	curIsParent = false;

	btnListHorRight: Array<cc.Button> = new Array<cc.Button>();
	btnListHorLeft: Array<cc.Button> = new Array<cc.Button>();
	btnListVerUp: Array<cc.Button> = new Array<cc.Button>();
	btnListVerDown: Array<cc.Button> = new Array<cc.Button>();
	@property
	savePrePanelList: Array<cc.Node> = new Array<cc.Node>();
	@property
	moveBtns: Array<cc.Button> = new Array<cc.Button>();

	isEnterInput: boolean = false;
	enterInputValue: number = 0;
	offSetTime: number = 0.5;

	gameCount: number = 1;

	enterInputActions = new Map();
	backInputActions = new Map();
	inputDirections = new Map();
	keycodeNumActions = new Map();
	curBtnChangeActions = new Map();
	protected onLoad(): void {
		cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
		cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
	}

	addCurBtnChangeAction(callName: string, callBack: Function): void {
		if (this.curBtnChangeActions && !this.curBtnChangeActions.has(callName))
			this.curBtnChangeActions.set(callName, callBack);
	}
	removeCurBtnChangeAction(callName: string) {
		if (this.curBtnChangeActions && this.curBtnChangeActions.has(callName))
			this.curBtnChangeActions.delete(callName);
	}
	//添加Enter键事件
	addEnterInputAction(callName: string, callBack: Function): void {
		if (this.enterInputActions && !this.enterInputActions.has(callName))
			this.enterInputActions.set(callName, callBack);
	}
	removeEnterInputAction(callName: string) {
		if (this.enterInputActions && this.enterInputActions.has(callName))
			this.enterInputActions.delete(callName);
	}
	//添加Back键事件
	addBackInputAction(callName: string, callBack: Function): void {
		if (this.backInputActions && !this.backInputActions.has(callName))
			this.backInputActions.set(callName, callBack);
	}
	removeBackInputAction(callName: string) {
		if (this.backInputActions && this.backInputActions.has(callName))
			this.backInputActions.delete(callName);
	}

	//添加方向键移动事件
	addInputDirection(callName: string, callBack: Function): void {
		if (this.inputDirections && !this.inputDirections.has(callName))
			this.inputDirections.set(callName, callBack);
	}

	removeInputDirection(callName: string) {
		if (this.inputDirections && this.inputDirections.has(callName))
			this.inputDirections.delete(callName);
	}

	//添加数字键事件
	addKeycodeNumAction(callName: string, callBack: Function): void {
		if (this.keycodeNumActions && !this.keycodeNumActions.has(callName))
			this.keycodeNumActions.set(callName, callBack);
	}

	removeKeycodeNumAction(callName: string) {
		if (this.keycodeNumActions && this.keycodeNumActions.has(callName))
			this.keycodeNumActions.delete(callName);
	}

	protected update(dt: number): void {
		if (this.isEnterInput) {
			this.enterInputValue = this.enterInputValue + dt;
			if (this.enterInputValue > this.offSetTime) {
				this.isEnterInput = false
				this.enterInputValue = 0
			}


		}

	}


	onKeyDown(event: cc.Event.EventKeyboard): void {
		if (this.inputDirections.size > 0 && this.isCanMoveForword)
			this.onKeyDownDirection(event);
		if (this.curInputKey != null)
			return;
		this.curInputKey = event.keyCode
		switch (event.keyCode) {
			case cc.macro.KEY.enter: case cc.macro.KEY.y: case inputKeyCode.enter: case inputKeyCode.enter2:
				if (!this.curBtn) {
					this.keycodeNumActions.forEach((key) => {
						key(event.keyCode);
					});
					return;
				}
				if (!this.isEnterInput) {
					this.enterInputValue = 0
					this.isEnterInput = true;
					cc.error("确定键按下::" + this.curBtn.node.name);
					if (this.curBtn && this.curBtn.getComponent(cc.Toggle)) {
						var _toggle = this.curBtn.getComponent(cc.Toggle);
						_toggle.isChecked = !_toggle.isChecked
						_toggle.checkEvents.forEach(element => {
							cc.error("事件名字::" + element.handler);
							element.emit([element.handler, element.customEventData, this.curBtn]);
						});
					}
					else {
						if (this.curBtn.clickEvents.length>0)
						{
							this.curBtn.clickEvents.forEach(element => {
								cc.error("事件名字::" + element.handler);
								element.emit([element.handler, element.customEventData, this.curBtn]);
							});
						}
						else
						{

							cc.Component.EventHandler.emitEvents(this.curBtn.clickEvents);
							this.curBtn.node.emit('click', this.curBtn);
						}

					}
				}

				break;
			case cc.macro.KEY.escape: case inputKeyCode.quit: case cc.macro.KEY.backspace:
				cc.error("返回键按下");
				this.backInputActions.forEach((key) => {
					key();
				});

				break;
			case cc.macro.KEY.right: case inputKeyCode.right:
				// cc.error("右键按下");
				this.setNavigationBtnEvent(true, 1, this.curBtn);
				break;
			case cc.macro.KEY.left: case inputKeyCode.left:
				// cc.error("左键按下");
				this.setNavigationBtnEvent(true, -1, this.curBtn);
				break;
			case cc.macro.KEY.up: case inputKeyCode.up:
				// cc.error("上键按下");
				this.setNavigationBtnEvent(false, 1, this.curBtn);
				break;
			case cc.macro.KEY.down: case inputKeyCode.down:
				// cc.error("下键按下");
				this.setNavigationBtnEvent(false, -1, this.curBtn);
				break;
			case inputKeyCode.num0: case inputKeyCode.num1: case inputKeyCode.num2:
			case inputKeyCode.num3: case inputKeyCode.num4: case inputKeyCode.num5:
			case inputKeyCode.num6: case inputKeyCode.num7: case inputKeyCode.num8: case inputKeyCode.num9:
				// cc.error("数字键按下::"+event.keyCode);
				this.keycodeNumActions.forEach((key) => {
					key(event.keyCode);
				});
				break;
			case cc.macro.KEY.m:
				cc.director.resume();
				MonoSingle.GetBaseInputEvent().showBtnsState();
				break;
			case cc.macro.KEY.n:
				cc.director.pause();
				break;
		}
	}

	onKeyUp(event: cc.Event.EventKeyboard): void {
		this.curInputKey = null;
		this.inputDirections.forEach((key) => {
			key(cc.v2(0, 0), inputDirectionKey.none);
		});
	}

	inputDirectionsFunction(forword, directionKey) {
		this.inputDirections.forEach((key) => {
			key(forword, directionKey);
		});
	}

	onKeyDownDirection(event: cc.Event.EventKeyboard): void {
		let directionKey = inputDirectionKey.none;
		let forword = cc.v2(0, 0);

		switch (event.keyCode) {
			case cc.macro.KEY.right: case inputKeyCode.right:
				directionKey = inputDirectionKey.right;
				forword = cc.v2(1, 0);
				// this.setNavigationBtnEvent(true, 1, this.curBtn);
				this.inputDirectionsFunction(forword, directionKey)
				break;
			case cc.macro.KEY.left: case inputKeyCode.left:
				directionKey = inputDirectionKey.left;
				forword = cc.v2(-1, 0);
				// this.setNavigationBtnEvent(true, -1, this.curBtn);
				this.inputDirectionsFunction(forword, directionKey)
				break;
			case cc.macro.KEY.up: case inputKeyCode.up:
				directionKey = inputDirectionKey.up;
				forword = cc.v2(0, 1);
				// this.setNavigationBtnEvent(false, 1, this.curBtn);
				this.inputDirectionsFunction(forword, directionKey)
				break;
			case cc.macro.KEY.down: case inputKeyCode.down:
				directionKey = inputDirectionKey.down;
				forword = cc.v2(0, -1);
				// this.setNavigationBtnEvent(false, -1, this.curBtn);
				this.inputDirectionsFunction(forword, directionKey)
				break;
			case inputKeyCode.enter: case inputKeyCode.enter2:
				directionKey = inputDirectionKey.enter;
				forword = cc.v2(0, 0);
				this.inputDirectionsFunction(forword, directionKey)
				break;
			// case inputKeyCode.num0: case inputKeyCode.num1: case inputKeyCode.num2:
			// case inputKeyCode.num3: case inputKeyCode.num4: case inputKeyCode.num5:
			// case inputKeyCode.num6: case inputKeyCode.num7: case inputKeyCode.num8: case inputKeyCode.num9:
			// 	forword = cc.v2(0, 0);
			// 	this.inputDirectionsFunction(forword, event.keyCode)
			// 	break;
		}

	}


	public arraySortEvent() {
		return;
		///////////右
		let btns = new Array<cc.Button>();
		MonoSingle.GetBaseInputEvent().btns.forEach(element => {
			btns.push(element)
		});
		btns.sort((a, b) => {
			if (this.getPos(a.node).y - this.getPos(b.node).y > 0)
				return this.getPos(b.node).y - this.getPos(a.node).y;
			else if (this.getPos(a.node).y - this.getPos(b.node).y == 0)
				return this.getPos(a.node).x - this.getPos(b.node).x;
		});
		btns.forEach(element => {
			this.btnListHorRight.push(element)
		});

		///////////左
		btns = new Array<cc.Button>();
		MonoSingle.GetBaseInputEvent().btns.forEach(element => {
			btns.push(element)
		});
		btns.sort((a, b) => {
			if (this.getPos(a.node).y - this.getPos(b.node).y > 0)
				return this.getPos(b.node).y - this.getPos(a.node).y;
			else if (this.getPos(a.node).y - this.getPos(b.node).y == 0)
				return this.getPos(a.node).x - this.getPos(b.node).x;
		});
		btns.forEach(element => {
			this.btnListHorLeft.push(element)
		});


		///////////上
		btns = new Array<cc.Button>();
		MonoSingle.GetBaseInputEvent().btns.forEach(element => {
			btns.push(element)
		});
		btns.sort((a, b) => {
			if (this.getPos(a.node).x - this.getPos(b.node).x < 0)
				return this.getPos(a.node).x - this.getPos(b.node).x;
			else if (this.getPos(b.node).x - this.getPos(a.node).x == 0)
				return this.getPos(b.node).y - this.getPos(a.node).y;
		});
		btns.forEach(element => {
			this.btnListVerUp.push(element)
		});
		///////////下
		btns = new Array<cc.Button>();
		MonoSingle.GetBaseInputEvent().btns.forEach(element => {
			btns.push(element)
		});
		btns.sort((a, b) => {
			if (this.getPos(a.node).x - this.getPos(b.node).x < 0)
				return this.getPos(a.node).x - this.getPos(b.node).x;
			else if (this.getPos(b.node).x - this.getPos(a.node).x == 0)
				return this.getPos(b.node).y - this.getPos(a.node).y;
		});
		btns.forEach(element => {
			this.btnListVerDown.push(element)
		});
	}

	getPos(node: cc.Node): cc.Vec2 {
		var spacePos = node.parent.convertToWorldSpaceAR(node.getPosition());
		var x = spacePos.x.toFixed(0);
		var y = spacePos.y.toFixed(0);
		spacePos = cc.v2(parseInt(x), parseInt(y))
		if (node != null)
			return spacePos;
	}

	initTvBoxEvent(timer: number = 0, isClearFinger: boolean = false) {
		if (isClearFinger)
			MonoSingle.GetTvBoxButtonListEvent().clearFingerEvent();
		if (timer == 0) {
			MonoSingle.GetBaseInputEvent().InitTvBoxButtonListEvent();
			return
		}
		this.scheduleOnce(function () {
			MonoSingle.GetBaseInputEvent().InitTvBoxButtonListEvent();
		},
			timer)
	}
	initTvBoxCurPanelEvent(timer: number = 0, curPanel: cc.Node = null, _this: any = null) {
		cc.error(curPanel, "刷新界面")
		var self = this;
		if (timer == 0) {
			self.curPanel = curPanel;
			MonoSingle.GetBaseInputEvent().InitTvBoxButtonListEvent();
			return
		}
		if (_this != null) {
			_this.scheduleOnce(function () {
				self.curPanel = curPanel;
				MonoSingle.GetBaseInputEvent().InitTvBoxButtonListEvent();
			},
				timer)
			return
		}
		this.scheduleOnce(function () {
			self.curPanel = curPanel;
			MonoSingle.GetBaseInputEvent().InitTvBoxButtonListEvent();
		},
			timer)
	}
	scheduleOnceEvent(timer: number = 0, _functionAction: Function = null) {
		this.scheduleOnce(function () {
			_functionAction()
		},
			timer)
	}
	selectCurBtn(timer: number = 0, tempBtn: cc.Button) {

		if (!MonoSingle.GetBaseInputEvent().isParentsActive(tempBtn.node)) {
			if (timer == 0) {
				MonoSingle.GetBaseInputEvent().InitTvBoxButtonListEvent();
			}
			else {
				this.scheduleOnce(function () {
					MonoSingle.GetBaseInputEvent().InitTvBoxButtonListEvent();
				},
					timer)
			}
			return
		}

		if (timer == 0) {
			MonoSingle.GetBaseInputEvent().InitTvBoxButtonListEvent(false);
			this.curBtn = tempBtn;
			MonoSingle.GetTvBoxButtonListEvent().SetBtnListEventUISelectState();
			return
		}
		this.scheduleOnce(function () {
			MonoSingle.GetBaseInputEvent().InitTvBoxButtonListEvent(false);
			this.curBtn = tempBtn;
			MonoSingle.GetTvBoxButtonListEvent().SetBtnListEventUISelectState();
		},
			timer)
	}
	getSavePrePanelTop(): cc.Node {
		if (this.savePrePanelList.length != 0) {
			return this.savePrePanelList[this.savePrePanelList.length - 1]
		}

		else
			return null
	}
	pushSavePrePanelTop(_node: cc.Node) {
		if (this.savePrePanelList.indexOf(_node) < 0)
			this.savePrePanelList.push(_node)
	}
	popSavePrePanelTop() {
		if (this.savePrePanelList.length != 0)
			this.savePrePanelList.pop();
	}

	clearMoveBtns() {
		this.moveBtns = []
	}

	Reset() {
		this.btnListHorRight = new Array<cc.Button>();
		this.btnListHorLeft = new Array<cc.Button>();
		this.btnListVerUp = new Array<cc.Button>();
		this.btnListVerDown = new Array<cc.Button>();
		this.savePrePanelList = new Array<cc.Node>();
		this.enterInputActions = new Map();
		this.backInputActions = new Map();
		this.inputDirections = new Map();
		this.keycodeNumActions = new Map();
		this.curBtnChangeActions = new Map();
		this.curBtn = null
		MonoSingle.GetBaseInputEvent().btns = new Array<cc.Button>();
	}

	pushRefreshPanel(timer: number = 0, _curPanel: cc.Node = null, _tempBtn: cc.Button = null) {
		var self = this;
		self.curBtn = null;
		cc.error(_curPanel, "添加界面");
		MonoSingle.GetTvBoxButtonListEvent().clearFingerEvent();
		MonoSingle.GetTvBoxButtonListEvent().ResetBtnListEvent();

		this.scheduleOnce(function () {
		
			if (_curPanel && _curPanel.active == !1)
				return;
			if (this.savePrePanelList.indexOf(_curPanel) < 0 && _curPanel != null && _curPanel.active == !0)
				this.savePrePanelList.push(_curPanel)
			self.curPanel = _curPanel;
			if (_tempBtn) {
				MonoSingle.GetBaseInputEvent().InitTvBoxButtonListEvent(false);
				self.curBtn = _tempBtn;
				MonoSingle.GetTvBoxButtonListEvent().SetBtnListEventUISelectState();
			}
			else {
				MonoSingle.GetBaseInputEvent().InitTvBoxButtonListEvent();
			}
		}, timer)
	}

	popRefreshPanel(timer: number = 0, _curPanel: cc.Node = null) {
		var self = this;
		cc.error(_curPanel, "删除界面");
		self.curBtn = null;
		MonoSingle.GetTvBoxButtonListEvent().clearFingerEvent();
		MonoSingle.GetTvBoxButtonListEvent().ResetBtnListEvent();
		var func = function () {
		
			for (let i = 0; i < self.savePrePanelList.length; i++) {
				let vaule = self.savePrePanelList[i];
				if (vaule == _curPanel) {
					self.savePrePanelList.splice(i, 1), self.curPanel = self.getSavePrePanelTop(),
						MonoSingle.GetBaseInputEvent().InitTvBoxButtonListEvent();
					break;
				}
			}
		}
		if (timer = 0) {
			func();
			return;
		}
		this.scheduleOnce(function () {
			func();
		}, timer)
	}

	refreshPanel(timer: number = 0, _tempBtn: cc.Button = null) {
		var self = this;
		cc.error(self.getSavePrePanelTop(), "刷新界面");
		self.curBtn = null;
		MonoSingle.GetTvBoxButtonListEvent().clearFingerEvent();
		MonoSingle.GetTvBoxButtonListEvent().ResetBtnListEvent();
		this.scheduleOnce(function () {
			self.getSavePrePanelTop();
			if (_tempBtn) {
				MonoSingle.GetBaseInputEvent().InitTvBoxButtonListEvent(false);
				self.curBtn = _tempBtn;
				MonoSingle.GetTvBoxButtonListEvent().SetBtnListEventUISelectState();
			}
			else {
				MonoSingle.GetBaseInputEvent().InitTvBoxButtonListEvent();
			}
		}, timer)
	}


	setNavigationBtnEvent(isHor: boolean = true, value: number = 1, tempBtn: cc.Button) {
		if (this.curBtn == null)
			return;


		let disValue = 99999;
		let _tempBtn = null
		let _tempBtns = []
		if (isHor) {
			if (value > 0) {
				//右
				MonoSingle.GetBaseInputEvent().btns.forEach(element => {
					if (this.curBtn != element && this.getPos(this.curBtn.node).y == this.getPos(element.node).y && this.getPos(this.curBtn.node).x < this.getPos(element.node).x)
						_tempBtns.push(element);
				});
				if (_tempBtns.length == 0)
					_tempBtns = MonoSingle.GetBaseInputEvent().btns;

				_tempBtns.forEach(element => {
					// && curVec.y >= eleVec.y
					//&& Math.abs(curVec.x - eleVec.x) > 10
					let curVec = this.getPos(this.curBtn.node);
					let eleVec = this.getPos(element.node);
					if (curVec.x < eleVec.x && Math.abs(curVec.x - eleVec.x) > 10) {
						let tempDis = cc.Vec2.distance(curVec, eleVec);
						if (disValue > tempDis) {
							disValue = tempDis;
							_tempBtn = element;
						}
						else if (disValue == tempDis) {
							if (eleVec.y > this.getPos(_tempBtn.node).y) {
								_tempBtn = element;
							}
						}
					}
				});
				if (_tempBtn != null)
					this.curBtn = _tempBtn;
				MonoSingle.GetTvBoxButtonListEvent().SetBtnListEventUISelectState();
				this.curBtnChangeActions.forEach((key) => {
					key(this.curBtn);
				});
			}
			else {
				//左
				MonoSingle.GetBaseInputEvent().btns.forEach(element => {
					if (this.curBtn != element && this.getPos(this.curBtn.node).y == this.getPos(element.node).y && this.getPos(this.curBtn.node).x > this.getPos(element.node).x)
						_tempBtns.push(element);
				});
				if (_tempBtns.length == 0) 
					_tempBtns = MonoSingle.GetBaseInputEvent().btns;

				_tempBtns.forEach(element => {
					//&& curVec.y <= eleVec.y
					//&& Math.abs(curVec.x - eleVec.x) > 10
					let curVec = this.getPos(this.curBtn.node);
					let eleVec = this.getPos(element.node);
					if (curVec.x > eleVec.x && Math.abs(curVec.x - eleVec.x) > 10 ) {
						let tempDis = cc.Vec2.distance(curVec, eleVec);
						if (disValue > tempDis) {
							disValue = tempDis;
							_tempBtn = element;
						}
						else if (disValue == tempDis) {
							if (eleVec.y > this.getPos(_tempBtn.node).y) {
								_tempBtn = element;
							}
						}

					}

				});
				if (_tempBtn != null)
					this.curBtn = _tempBtn;
				MonoSingle.GetTvBoxButtonListEvent().SetBtnListEventUISelectState();
				this.curBtnChangeActions.forEach((key) => {
					key(this.curBtn);
				});
			}
		}
		else {
			if (value > 0) {
				//上
				MonoSingle.GetBaseInputEvent().btns.forEach(element => {
					if (this.curBtn != element && this.getPos(this.curBtn.node).x == this.getPos(element.node).x && this.getPos(this.curBtn.node).y < this.getPos(element.node).y)
						_tempBtns.push(element);
				});
				if (_tempBtns.length == 0)
					_tempBtns = MonoSingle.GetBaseInputEvent().btns;

				_tempBtns.forEach(element => {
					//&& Math.abs(curVec.y - eleVec.y) > 10
					let curVec = this.getPos(this.curBtn.node);
					let eleVec = this.getPos(element.node);
					if (curVec.y < eleVec.y && curVec.x >= eleVec.x ) {
						let tempDis = cc.Vec2.distance(curVec, eleVec);
						if (disValue > tempDis) {
							disValue = tempDis;
							_tempBtn = element;
						}
						else if (disValue == tempDis) {
							if (eleVec.x < this.getPos(_tempBtn.node).x) {
								_tempBtn = element;
							}
						}
					}
				});
				if (_tempBtn != null)
					this.curBtn = _tempBtn;
				MonoSingle.GetTvBoxButtonListEvent().SetBtnListEventUISelectState();
				this.curBtnChangeActions.forEach((key) => {
					key(this.curBtn);
				});
			}
			else {
				//下
				MonoSingle.GetBaseInputEvent().btns.forEach(element => {
					if (this.curBtn != element && this.getPos(this.curBtn.node).x == this.getPos(element.node).x && this.getPos(this.curBtn.node).y > this.getPos(element.node).y)
						_tempBtns.push(element);
				});
				if (_tempBtns.length == 0)
					_tempBtns = MonoSingle.GetBaseInputEvent().btns;

				_tempBtns.forEach(element => {

					//&& Math.abs(curVec.y - eleVec.y) > 10
					let curVec = this.getPos(this.curBtn.node);
					let eleVec = this.getPos(element.node);
					if (curVec.y > eleVec.y && curVec.x <= eleVec.x) {
						let tempDis = cc.Vec2.distance(curVec, eleVec);
						if (disValue > tempDis) {
							disValue = tempDis;
							_tempBtn = element;
						}
						else if (disValue == tempDis) {
							if (eleVec.x < this.getPos(_tempBtn.node).x) {
								_tempBtn = element;
							}
						}
					}
				});
				if (_tempBtn != null)
					this.curBtn = _tempBtn;
				MonoSingle.GetTvBoxButtonListEvent().SetBtnListEventUISelectState();
				this.curBtnChangeActions.forEach((key) => {
					key(this.curBtn);
				});
			}
		}


		return;
		if (isHor) {
			if (value > 0) {
				// this.btnListHorRight.forEach(element => {
				//     cc.error("right::" + element.name);
				// });
				for (let index = 0; index < this.btnListHorRight.length; index++) {
					const element = this.btnListHorRight[index];
					if (element == tempBtn && this.btnListHorRight[index + 1]) {
						let offa = this.getPos(this.curBtn.node).x + this.curBtn.node.getContentSize().width / 2 * (this.curBtn.node.getComponent(GetScale) ? this.curBtn.getComponent(GetScale).scale : this.curBtn.node.scaleX);
						let offb = this.getPos(this.btnListHorRight[index + 1].node).x - this.btnListHorRight[index + 1].node.getContentSize().width / 2 *
							(this.btnListHorRight[index + 1].node.getComponent(GetScale) ? this.btnListHorRight[index + 1].getComponent(GetScale).scale : this.btnListHorRight[index + 1].node.scaleX);
						if (offb - offa >= 0) {
							// cc.error(this.btnListHorRight[index + 1].name);
							this.curBtn = this.btnListHorRight[index + 1];
							MonoSingle.GetTvBoxButtonListEvent().SetBtnListEventUISelectState();
							MonoSingle.GetTvBoxInput().curBtnChangeActions.forEach((key) => {
								key(MonoSingle.GetTvBoxInput().curBtn);
							});
						}
						// else
						//     this.setNavigationBtnEvent(true, 1, this.btnListHorRight[index + 1]);
					}
				}
			}
			else {
				// this.btnListHorLeft.forEach(element => {
				//     cc.error("left::" + element.name);
				// });
				for (let index = this.btnListHorLeft.length - 1; index >= 0; index--) {
					const element = this.btnListHorLeft[index];
					if (element == tempBtn && this.btnListHorLeft[index - 1]) {
						let offa = this.getPos(this.curBtn.node).x - this.curBtn.node.getContentSize().width / 2 * (this.curBtn.node.getComponent(GetScale) ? this.curBtn.getComponent(GetScale).scale : this.curBtn.node.scaleX);
						let offb = this.getPos(this.btnListHorLeft[index - 1].node).x + this.btnListHorLeft[index - 1].node.getContentSize().width / 2 *
							(this.btnListHorRight[index - 1].node.getComponent(GetScale) ? this.btnListHorRight[index - 1].getComponent(GetScale).scale : this.btnListHorRight[index - 1].node.scaleX);
						if (offa - offb >= 0) {
							// cc.error(this.btnListHorLeft[index - 1].name);
							this.curBtn = this.btnListHorLeft[index - 1];
							MonoSingle.GetTvBoxButtonListEvent().SetBtnListEventUISelectState();
							MonoSingle.GetTvBoxInput().curBtnChangeActions.forEach((key) => {
								key(MonoSingle.GetTvBoxInput().curBtn);
							});
						}
						// else
						//     this.setNavigationBtnEvent(true, -1, this.btnListHorLeft[index - 1]);
					}
				}
			}

		}
		else {
			if (value > 0) {
				// this.btnListVerUp.forEach(element => {
				//     cc.error("up::" + element.name);
				// });
				for (let index = this.btnListVerUp.length - 1; index >= 0; index--) {
					const element = this.btnListVerUp[index];
					if (element == tempBtn && this.btnListVerUp[index - 1]) {
						let offa = this.getPos(this.curBtn.node).y + this.curBtn.node.getContentSize().height / 2 * (this.curBtn.node.getComponent(GetScale) ? this.curBtn.getComponent(GetScale).scale : this.curBtn.node.scaleY);
						let offb = this.getPos(this.btnListVerUp[index - 1].node).y + this.btnListVerUp[index - 1].node.getContentSize().height / 2 *
							(this.btnListHorRight[index - 1].node.getComponent(GetScale) ? this.btnListHorRight[index - 1].getComponent(GetScale).scale : this.btnListHorRight[index - 1].node.scaleY);
						if (offb - offa >= 0) {
							// cc.error(this.btnListVerUp[index - 1].name);
							this.curBtn = this.btnListVerUp[index - 1];
							MonoSingle.GetTvBoxButtonListEvent().SetBtnListEventUISelectState();
							MonoSingle.GetTvBoxInput().curBtnChangeActions.forEach((key) => {
								key(MonoSingle.GetTvBoxInput().curBtn);
							});
						}
						// else
						//     this.setNavigationBtnEvent(false, 1, this.btnListVerUp[index - 1]);
					}
				}
			}
			else {
				// this.btnListVerDown.forEach(element => {
				//     cc.error("down::" + element.node.parent.name);
				// });
				for (let index = 0; index < this.btnListVerDown.length; index++) {
					const element = this.btnListVerDown[index];
					if (element == tempBtn && this.btnListVerDown[index + 1]) {
						let offa = this.getPos(this.curBtn.node).y - this.curBtn.node.getContentSize().height / 2 * (this.curBtn.node.getComponent(GetScale) ? this.curBtn.getComponent(GetScale).scale : this.curBtn.node.scaleY);
						let offb = this.getPos(this.btnListVerDown[index + 1].node).y - this.btnListVerDown[index + 1].node.getContentSize().height / 2 *
							(this.btnListHorRight[index + 1].node.getComponent(GetScale) ? this.btnListHorRight[index + 1].getComponent(GetScale).scale : this.btnListHorRight[index + 1].node.scaleY);
						if (offa - offb >= 0) {
							// cc.error(this.btnListVerDown[index + 1].name);
							this.curBtn = this.btnListVerDown[index + 1];
							MonoSingle.GetTvBoxButtonListEvent().SetBtnListEventUISelectState();
							MonoSingle.GetTvBoxInput().curBtnChangeActions.forEach((key) => {
								key(MonoSingle.GetTvBoxInput().curBtn);
							});
						}
						// else
						//     this.setNavigationBtnEvent(false, -1, this.btnListVerDown[index + 1]);
					}
				}
			}
		}
	}
}
